/*
 * @Author: Starfish_DL
 * @Date: 2020-11-10 15:57:56
 * @LastEditors: Starfish_DL
 * @LastEditTime: 2020-11-10 16:38:12
 * @CurrentVersion: 1.0
 * @Description: 
 */

class Account {

    private String accountId;
    private double balance;

    public Account() {
    }

    public Account(String accountId, double balance) {
        this.accountId = accountId;
        this.balance = balance;
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Account other = (Account) obj;
        if (accountId == null) {
            if (other.accountId != null)
                return false;
        } else if (!accountId.equals(other.accountId))
            return false;
        if (Double.doubleToLongBits(balance) != Double.doubleToLongBits(other.balance))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        // todo hashCode函数的重载
        final int prime = 31;
        int result = 1;
        result = prime * result + ((accountId == null) ? 0 : accountId.hashCode());
        long temp;
        temp = Double.doubleToLongBits(balance);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
}

class GetMoney extends Thread {

    Account account;
    double withdrawal;

    public GetMoney(String name, Account account, double money) {
        super(name);
        this.account = account;
        this.withdrawal = money;
    }

    @Override
    public void run() {
        synchronized (account) {
            if (this.account.getBalance() >= this.withdrawal) {
                this.account.setBalance(this.account.getBalance() - this.withdrawal);
                System.out.println("取款成功，您当前的存款为：" + account.getBalance());
            } else {
                System.out.println("暂无这么多的存款");
            }
        }
    }
}

public class TestAccount {
    public static void main(String[] args) {
        Account account1 = new Account("043", 1000);
        GetMoney get1 = new GetMoney("withdraw1", account1, 500);
        get1.start();
    }
}
